import { _get, _post, _post1 } from "../../utils/request";
import util from "../../utils/util";
import debounce, { throttle } from "../../utils/debounce"
const app = getApp();

Page({
    data: {
        tabs: [{
                title: "领取设备",
                badgeType: "text",
                name: "xx",
                // badgeText: "6",
            },
            {
                title: "我的设备",
                badgeType: "dot",
                name: "aa",
            },
        ],
        activeTab: 0,
        table_borrow: [
            // {
            //     id: 0,
            //     name: "音频信号发生器",
            // },
            // {
            //     id: 1,
            //     name: "音频信号发生器",
            // },
            // {
            //     id: 2,
            //     name: "音频信号发生器",
            // },
            // {
            //     id: 3,
            //     name: "音频信号发生器",
            // },
        ],
        table_return: [{
                id: 0,
                name: "音频信号发生器",
            },
            {
                id: 1,
                name: "音频信号发生器",
            },
            {
                id: 2,
                name: "音频信号发生器",
            },
            {
                id: 3,
                name: "音频信号发生器",
            },
        ],

        showBottom1: false,
        confirm: [],
        showBottom2: false,
        show_return: false,
        return: [{ name: '1', value: '正常', checked: true },
            { name: '2', value: '不正常' },
        ]
    },

    handleTabClick({ index }) {
        this.setData({
            activeTab: index,
        });
    },
    search(e) {
        this.getEquip1(e)
    },
    //获取设备列表
    getEquip1(keywords = '') {
        let params = {
            equipmentName: keywords
        }
        _get(`/equipment/selectEquipment`, params).then(res => {
            console.log('equip_list1', res);
            this.setData({
                table_borrow: res.data
            })
        })
    },
    getEquip2(keywords = '') {
        let params = {
            userId: app.userId,
            equipmentName: keywords
        }
        _get(`/equipment/selectMyEquipment`, params).then(res => {
            console.log('equip_list2', res);
            this.setData({
                table_return: res.data
            })
        })
    },
    borrow(e) {
        console.log('领取设备', e);
        app.equipIndex = e.currentTarget.id
        this.setData({
            showBottom1: true,
        });
        // console.log('this', this);
        this.getTest();
    },
    borrow1: throttle(function(e) {
        console.log(e);
        let that = this
        dd.confirm({
            title: '',
            content: "领取" + this.data.table_borrow[app.equipIndex].equipmentname,
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            success(res) {
                if (res.confirm) {
                    let params = [{
                            equipmentId: that.data.table_borrow[app.equipIndex].id,
                            sourceValidDate: util.formatDatetime(new Date(e.timeStamp)),
                            userId: app.userId,
                            vbizId: e.currentTarget.id
                        }]
                        // console.log('领取成功,借设备params', params);
                    _post1(`/equipment/lend`, JSON.stringify(params)).then(res => {
                        console.log('领取以后', res);
                        dd.alert({
                            content: res.message ? res.message : '领取失败'
                        })
                    })
                    that.onPopupClose1();
                    // _post(`/equipment/lend`,params)
                } else {
                    console.log("取消操作");
                    that.onPopupClose1();
                }
            },
            fail() {

                // console.log("取消操作");
            },
            complete() {

            },
        });
    }, 2000),
    onPopupClose1() {
        this.setData({
            showBottom1: false,
        });
    },
    onPopupClose2() {
        this.setData({
            showBottom2: false,
        });
    },

    getTest() {
        let params = {
            userId: app.userId,
            workerName: ''
        }
        _get(`/equipment/select`, params).then((res) => {
            // console.log(res);
            // console.log('test', res.data);
            this.setData({
                test: res.data
            })

        })
    },
    return (e) {
        console.log('归还设备', e);
        app.equipId = e.currentTarget.id
        this.setData({
                show_return: true
            })
            // dd.confirm({
            //     // title: '',
            //     content: "归还" + this.data.table_return[e.currentTarget.id].name,
            //     confirmButtonText: "确定",
            //     cancelButtonText: "取消",
            //     success: (res) => {
            //         if (res.confirm) {
            //             console.log("归还成功");
            //         } else {
            //             console.log("取消操作");
            //         }
            //     },
            //     fail: () => {

        //     },
        //     complete: () => {},
        // });

    },
    //归还设备弹窗
    postConfirm(e) {
        console.log('确认设备归还', e);
        let params = {
            equipmentUseId: app.equipId,
            remark: e.detail.value.equip_remarks,
            useCondition: e.detail.value.usage,
            userId: app.userId
        }
        _post(`/equipment/returnEquipment`, params).then(res => {
                console.log('归还成功', res);
                this.setData({
                    show_return: false
                })
                this.getEquip1()
                this.getEquip2()
            },
            reason => {
                reject(reason)
            }
        )
    },
    cancel() {
        this.setData({
            show_return: false
        })
    },

    onLoad() {
        dd.setNavigationBar({
            title: "设备",
            backgroundColor: "#142e4f",
            success() {},
            fail() {},
        });
        this.getEquip1()
        this.getEquip2()
    },
});